package com.onegroup.common.handler;

import com.alibaba.fastjson2.JSON;
import com.onegroup.common.enums.ResponseStatusEnum;
import com.onegroup.common.utils.WebUtil;
import com.onegroup.pojo.ResponseResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: 曾雪斌
 * @Description: 对认证失败进行处理
 * @History:
 */
@Component
public class AuthenticationEntityPointImpl implements AuthenticationEntryPoint {
    /**
     * 对认证失败的用户请求进行处理
     * @param request 请求
     * @param response 响应
     * @param authException 认证失败异常
     * @throws IOException IO异常
     * @throws ServletException Servlet异常
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
            throws IOException, ServletException {
        ResponseResult<String> result = new ResponseResult<>(ResponseStatusEnum.AUTH_ERROR);
        String resultJson = JSON.toJSONString(result);
        WebUtil.renderString(response, resultJson);
    }
}
